/* test.c */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "protocol.h"
#include "log.h"
#include "process.h"
#include "user.h"
#include "ticket.h"

int main(){
	char *msg = "|1|222|xuyinfei|password|1133|111111|1111111|T506|3|Nothind|";
	char msgg[256];
	int i;
	protocol_t ptc;
	parse_msg( &ptc, msg);
	log_t log;
	logfile_t *logfile;	
	user_database_t * base;
	ticket_collection_t *col;
	// test protocol
	printf("\n%d\n%d\n%s\n%s\n%d\n%s\n%d\n%s\n",
			ptc.act_type,
			ptc.usr_id,
			ptc.usr_name,
			ptc.usr_pwd,
			ptc.order_no,
			ptc.train_no,
			ptc.ticket_count,
			ptc.other);
	format_msg(&ptc, msgg);
	printf(msgg);
	//test log
	printf("\n========test log========\n");
	log.order_id = 1;
	log.order_time = 1111111;
	strcpy(log.usr_name,"xuyinfei");
	strcpy(log.train_no,"T508");
	log.time = 55555555;
	log.ticket_num = 3;
	strcpy(log.other,"nothing");
	logfile = logfile_open("log.log");
	add_log(logfile, &log);
	logfile_close(logfile);
	printf("\n========end test log===========\n");
	//test user
	printf("\n========test user===========\n");
	base = user_database_open("data/user.dat");
	printf("check user %d", check_user(base, "kitty","000000"));
	printf("check user %d", check_user(base, "xuyinfei","000000"));
	printf("\n========end test user===========\n");
	printf("\n========test ticket===========\n");
	col = ticket_collection_open("data/ticket.dat");

	for(i = 0; i<col->len;i++){
		printf("%d,%s,%ld,%d,%s\n",col->tickets[i].id,col->tickets[i].trainno,col->tickets[i].time,col->tickets[i].num,col->tickets[i].description);
	}
	
	printf("\n========end test ticket===========\n");

	return 0;
}
